home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 14.7 KB | 595 lines | [TEXT/KAHL] |
- /*
- File: DialogWindow.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __BLJSTANDARDINCLUDES__
- #include "BLJStandardIncludes.h"
- #endif
-
- #ifndef __DIALOGWINDOW__
- #include "DialogWindow.h"
- #endif
-
- #ifndef __OBJECTLIST__
- #include "ObjectList.h"
- #endif
-
- /***********************************|****************************************/
-
- pascal OSErr SetDialogDefaultItem ( DialogPtr dP, short item ) = { 0x303c, 0x0304, 0xaa68 };
- pascal OSErr SetDialogCancelItem ( DialogPtr dP, short item ) = { 0x303c, 0x0305, 0xaa68 };
-
- /***********************************|****************************************/
-
- DeclareList(TDialogWindow,TDialogWindowList);
- ImplementList(TDialogWindow,TDialogWindowList,true);
-
- TDialogWindowList gTDialogWindowList;
-
- /***********************************|****************************************/
-
- Boolean TDialogWindow::HandleAnEvent ( EventRecord& event )
- {
- Boolean result = false;
-
- for ( unsigned short i = 1; i <= gTDialogWindowList.Count(); ++i ) {
- TDialogWindow * dialogWindow = gTDialogWindowList.Get ( i );
-
- if ( dialogWindow )
- if ( dialogWindow->HandleEvent ( event ) )
- {
- result = true;
- break;
- }
- }
-
- return result;
- }
-
- /***********************************|****************************************/
-
- typedef struct WindowInfoRecord {
- Boolean visible;
- Rect location;
- } WindowInfoRecord, *WindowInfoPtr, **WindowInfoHandle;
-
- /***********************************|****************************************/
-
- extern short gBovineServerPrefsFile;
-
- /***********************************|****************************************/
-
- Handle Get1ResourceFromFile ( OSType resType, short resID, short resFile )
- {
- short savedResFile = CurResFile ();
- UseResFile ( resFile );
- Handle result = Get1Resource ( resType, resID );
- UseResFile ( savedResFile );
-
- return result;
- }
-
- /***********************************|****************************************/
-
- Boolean GetDefaultWindowInformation ( short dialogID, Boolean& visible, Rect & screenRect )
- {
- WindowInfoHandle infoH = (WindowInfoHandle) Get1ResourceFromFile ( 'WINF', dialogID, gBovineServerPrefsFile );
-
- if ( infoH && *infoH )
- {
- visible = (**infoH).visible;
- screenRect = (**infoH).location;
-
- ReleaseResource ( (Handle) infoH );
- return true;
- }
-
- return false;
- }
-
- /***********************************|****************************************/
-
- void LocalToGlobalRect ( const Rect& rect, Rect & result )
- {
- result = rect;
- LocalToGlobal ( (Point *) & result.top );
- LocalToGlobal ( (Point *) & result.bottom );
- }
-
- /***********************************|****************************************
- *
- * SetDefaultWindowInformation ( WindowPtr dP, short dialogID )
- *
- * Save the location and other window state for the given window in a
- * resource of type 'WINF' with the resource id dialogID in the
- * application's preferences file.
- *
- ***********************************|***************************************/
-
- Boolean SetDefaultWindowInformation ( WindowPtr dP, short dialogID )
- {
- WindowInfoHandle infoH = (WindowInfoHandle) Get1ResourceFromFile ( 'WINF', dialogID, gBovineServerPrefsFile );
- GrafPtr savedPort;
- short savedResFile = CurResFile();
-
- UseResFile(gBovineServerPrefsFile);
- GetPort ( & savedPort );
- SetPort ( dP );
-
- if ( !infoH ) {
- infoH = (WindowInfoHandle) NewHandleClear ( sizeof( **infoH ) );
-
- AddResource ( (Handle) infoH, 'WINF', dialogID, "\p");
- }
-
- if ( infoH && *infoH )
- {
- (**infoH).visible = ((WindowPeek) dP)->visible;
- LocalToGlobalRect ( dP->portRect, (**infoH).location );
-
- ChangedResource ( (Handle) infoH );
- WriteResource ( (Handle) infoH );
- ReleaseResource ( (Handle) infoH);
-
- return true;
- }
-
- UseResFile (savedResFile);
- SetPort ( savedPort );
- return false;
- }
-
- /***********************************|****************************************
- *
- * RectOnScreen ( const Rect & where )
- *
- * Return true if the given rectangle is "on" the screen -- meaning that
- * no part of the rectangle is not on a visible screen.
- *
- ***********************************|***************************************/
-
- Boolean RectOnScreen ( const Rect& where )
- {
- RgnHandle rectRgn = NewRgn();
- RgnHandle intersectionRgn = NewRgn();
-
- RectRgn ( rectRgn, &where );
-
- SectRgn ( rectRgn, GetGrayRgn(), intersectionRgn );
-
- Boolean result = EqualRgn ( rectRgn, intersectionRgn );
-
- DisposeRgn ( rectRgn );
- DisposeRgn ( intersectionRgn );
-
- return result;
- }
-
- /***********************************|****************************************
- *
- * IsModalWindow ( WindowPtr window )
- *
- * Return true if this type of window is 'modal'; false otherwise.
- *
- ***********************************|***************************************/
-
- static Boolean IsModalWindow ( WindowPtr windowP )
- {
- Boolean result = false;
-
- if ( windowP )
- { short windowKind = ((WindowPeek) windowP)->windowKind;
-
- if ( ( windowKind == dBoxProc ) || ( windowKind == 5 ) )
- result = true;
- }
-
- return result;
- }
-
- /***********************************|****************************************/
-
- TDialogWindow::TDialogWindow ( short dialogID, short stringsSTRPoundID ) :
- fDialogID ( dialogID ),
- fStringsSTRPoundID ( stringsSTRPoundID ),
- fStringItemsCount ( 0 ),
- fButtonItemsCount ( 0 ),
- fUserItemsCount ( 0 ),
- fPosedModally ( false )
- {
- fDialogWindow = GetNewDialog ( dialogID, nil, (WindowPtr) -1 );
-
- if ( !fDialogWindow )
- return;
-
- // Try to get the 'default' location for this window from the preferences.
- Boolean visible;
- Rect where;
- if ( GetDefaultWindowInformation ( fDialogID, visible, where ) && RectOnScreen ( where ) )
- {
- MoveWindow ( fDialogWindow, where.left, where.top, false );
- }
- else
- visible = true;
-
- // Now, update the arrays of where each item is.
- short itemCount = CountDITL ( fDialogWindow );
- for ( short index = 1; index <= itemCount; ++index ) {
- short itemKind;
- Handle itemHandle;
- Rect itemRect;
-
- GetDItem ( fDialogWindow, index, & itemKind, & itemHandle, & itemRect );
-
- switch ( itemKind )
- {
- case statText:
- case editText:
- fStringItemNumbers [ ++ fStringItemsCount ] = index;
- break;
-
- case ctrlItem + btnCtrl:
- fButtonItemNumbers [ ++ fButtonItemsCount ] = index;
- break;
-
- case userItem:
- fUserItemNumbers [ ++ fUserItemsCount ] = index;
- fUserItemRect [ fUserItemsCount ] = itemRect;
- break;
- }
- }
-
- // If the first item is a button, then set it as the "default" item. Set the second
- // item as the cancel item if it's a button.
- if ( ( fButtonItemsCount >= 1 ) && ( fButtonItemNumbers[1] == 1 ) )
- {
- SetDialogDefaultItem ( fDialogWindow, 1 );
-
- if ( ( fButtonItemsCount >= 2 ) && ( fButtonItemNumbers[2] == 1 ) )
- {
- SetDialogCancelItem ( fDialogWindow, 2 );
- }
- }
-
- gTDialogWindowList.Append ( this );
-
- if ( visible )
- ::ShowWindow ( fDialogWindow );
-
- // If it's a modal window, pull it to the front.
- if ( IsModalWindow ( fDialogWindow ) )
- ::SelectWindow ( fDialogWindow );
- }
-
- /***********************************|****************************************/
-
- TDialogWindow::~TDialogWindow ( )
- {
- gTDialogWindowList.Remove ( this );
-
- DisposeDialog ( fDialogWindow );
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::ShowWindow ()
- {
- // If there's another dialog window frontmost, then wait for it to go away.
- if ( FrontWindow() && ( ((WindowPeek) FrontWindow())->windowKind == dialogKind ) )
- {
- TYield();
- }
-
- if ( fDialogWindow ) {
- ::ShowWindow ( fDialogWindow );
- SetPort ( fDialogWindow );
- SetDefaultWindowInformation ( fDialogWindow, fDialogID );
- }
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::SelectWindow ()
- {
- /* If there's another dialog window frontmost, then wait for it to go away.*/
- /* if ( FrontWindow() && ( ((WindowPeek) FrontWindow())->windowKind == dialogKind ) )*/
- /* {*/
- /* TYield();*/
- /* }*/
-
- if ( fDialogWindow ) {
- ::ShowWindow ( fDialogWindow );
- ::SelectWindow ( fDialogWindow );
- SetDefaultWindowInformation ( fDialogWindow, fDialogID );
- }
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::HideWindow ()
- {
- fPosedModally = false;
-
- if ( fDialogWindow )
- {
- ::HideWindow ( fDialogWindow );
- SetPort ( fDialogWindow );
- SetDefaultWindowInformation ( fDialogWindow, fDialogID );
- }
- }
-
- /***********************************|****************************************/
-
- static Handle GetDItemHandle ( DialogPtr dP, short whichItem )
- {
- short itemKind;
- Handle itemHandle;
- Rect itemRect;
-
- GetDItem ( dP, whichItem, & itemKind, & itemHandle, & itemRect );
-
- return itemHandle;
- }
-
- /***********************************|****************************************/
-
- static void SetItemText ( DialogPtr dP, short whichItem, unsigned char * item )
- {
- SetIText ( GetDItemHandle( dP, whichItem), item );
- }
-
- /***********************************|****************************************/
-
- static void SetItemText ( DialogPtr dP, short whichItem, char * item )
- {
- CStr255 itemStr255 = item;
-
- SetIText ( GetDItemHandle( dP, whichItem), itemStr255 );
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::SetItemText ( short whichItem, CStr255 item )
- {
- CStr255 itemStr255 = item;
-
- SetIText ( GetDItemHandle( fDialogWindow, whichItem), itemStr255 );
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::SetItemText ( short whichItem, Str255 item )
- {
- SetIText ( GetDItemHandle( fDialogWindow, whichItem), item );
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::SetTextItem ( short whichItem, const CStr255& itemName )
- {
- if (( 1 <= whichItem ) && ( whichItem <= fStringItemsCount ) )
- SetIText ( GetDItemHandle( fDialogWindow, fStringItemNumbers[whichItem] ), itemName );
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::SetTextItem ( short whichItem, short stringItemID )
- {
- Str255 value;
-
- if (( 1 <= whichItem ) && ( whichItem <= fStringItemsCount ) )
- {
- if ( stringItemID > 0 )
- GetIndString ( value, fStringsSTRPoundID, stringItemID );
- else
- value[0] = 0;
- SetIText ( GetDItemHandle( fDialogWindow, fStringItemNumbers[whichItem] ), value );
- }
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::GetTextItem ( short whichItem, CStr255& itemValue )
- {
- Str255 value = "\p";
-
- if ((1 <= whichItem ) && ( whichItem <= fStringItemsCount ))
- GetIText ( GetDItemHandle( fDialogWindow, fStringItemNumbers[whichItem]), value );
-
- itemValue = value;
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::DrawUserItem ( short i )
- {
- // Do nothing
- }
-
- /***********************************|****************************************/
-
- Boolean TDialogWindow::HandleEvent ( EventRecord & event )
- {
- Boolean result = false;
-
- // If we don't have a window, we don't need events.
- if ( ! fDialogWindow )
- return false;
-
- // Invisible windows don't get events.
- if ( !( (WindowPeek) fDialogWindow)->visible )
- return false;
-
- switch ( event.what )
- {
- case mouseDown:
- {
- Point where = event.where;
- WindowPtr windowPointedTo;
- short windowPart = FindWindow(where, &windowPointedTo);
-
- if ( (windowPointedTo == fDialogWindow))
- {
- HandleMouseDown ( event, windowPart );
- result = true;
- }
- else if ( IsModalWindow ( fDialogWindow ) && ( windowPointedTo != fDialogWindow ) )
- {
- SysBeep ( 60 );
- result = true;
- }
- }
- break;
-
- case nullEvent:
- if ( FrontWindow() == fDialogWindow )
- { short itemHit;
- WindowPtr windowPointedTo;
-
- DialogSelect ( & event, & windowPointedTo, & itemHit );
- result = false;
- }
- break;
-
- case keyDown:
- case keyUp:
- if ( FrontWindow() == fDialogWindow )
- { short itemHit;
- WindowPtr windowPointedTo;
-
- result = DialogSelect ( & event, & windowPointedTo, & itemHit );
- }
- break;
-
- case activateEvt:
- case updateEvt:
- if (WindowPtr(event.message) == fDialogWindow)
- {
- short itemHit;
- WindowPtr windowPointedTo;
-
- SetPort ( fDialogWindow );
- for ( unsigned short i = 1 ; i <= fUserItemsCount ; ++ i )
- DrawUserItem ( i );
- result = DialogSelect ( & event, & windowPointedTo, & itemHit );
- }
- break;
- }
-
- return result;
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::HandleMouseDown (EventRecord& event, short where )
- { GrafPtr savePort;
- Point pt = (Point) event.where;
-
- GetPort(&savePort);
- SetPort ( fDialogWindow );
-
- switch (where) {
- case inDrag:
- {
- Rect draggingRect;
-
- draggingRect.top = qd.screenBits.bounds.top;
- draggingRect.left = qd.screenBits.bounds.left;
- draggingRect.bottom = qd.screenBits.bounds.bottom - 4;
- draggingRect.right = qd.screenBits.bounds.right - 4;
-
- DragWindow(fDialogWindow, pt, &draggingRect);
-
- SetPort ( fDialogWindow );
- SetDefaultWindowInformation ( fDialogWindow, fDialogID );
- break;
- }
-
- case inGrow:
- break;
-
- case inGoAway:
- if (TrackGoAway( fDialogWindow, pt))
- HideWindow( );
- break;
-
- case inContent:
- {
- if (fDialogWindow == FrontWindow())
- {
- DialogPtr tempDialog;
- short itemHit;
- if ( DialogSelect ( &event, & tempDialog, & itemHit ) )
- {
- // If it's a button that's been pressed, then call HandleButtonPress()
- for ( unsigned short i = 1; i <= fButtonItemsCount; ++i )
- if ( fButtonItemNumbers [ i ] == itemHit )
- {
- HandleButtonPress ( i );
- break;
- }
- }
-
- }
- else
- ::SelectWindow( fDialogWindow );
-
- break;
- }
- }
-
- SetPort(savePort);
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::HandleButtonPress ( short whichButton )
- {
- if ( ( whichButton == 1 ) && ( fButtonItemsCount >= 1 ) && ( fButtonItemNumbers[1] == 1 ) )
- {
- fPosedModally = false;
- fItemWhichDismissedDialog = 1;
- return;
- };
-
- if ( ( whichButton == 2 ) && ( fButtonItemsCount >= 2 ) && ( fButtonItemNumbers[2] == 2 ) )
- {
- fPosedModally = false;
- fItemWhichDismissedDialog = 2;
- return;
- };
- }
-
- /***********************************|****************************************/
-
- void TDialogWindow::SetButtonTitle ( short whichButton, Str255 buttonTitle )
- {
- if ( ( whichButton >= 1 ) && ( whichButton <= fButtonItemsCount ) )
- {
- SetCTitle ( (ControlHandle) GetDItemHandle ( fDialogWindow, fButtonItemNumbers[whichButton] ), buttonTitle );
- }
- }
-
- /***********************************|****************************************/
-
- short TDialogWindow::PoseModally ( )
- {
- fPosedModally = true;
-
- while ( fPosedModally )
- TYield();
-
- return fItemWhichDismissedDialog;
- }
-
- /***********************************|****************************************/
-